<?php
	class database
	{
		public $host     = "" ; 
		public $user     = "" ;
		public $pass     = "" ; 
		public $database = "" ; 
		public $affected_rows = '' ;
		public $link_id = ''  ;
		public $query_id = '' ;
		public $last_id = '' ;
		public $num_rows='';
		public $query_counter = 0;
		
		//Timing Mechanism
		protected $measure_time;//true if we are doing timings 
		protected $start_time;//stotes the start clock
		protected $stop_time;//stores the stop clock
		private $time_str ='';//this stores the text for the debugger
		
		//Timing Results
		public $longest_query=0;
		public $longest_query_time=0;
		public $total_query_time=0;
		
		//mem cache upgrade
		protected $memcache;
		
		//transaction safe
		private $transaction_started;
		private $transaction_check;
		private $transaction_success;

		public function database()
		{
			$this->constructor();
		}
		
		public function __construct()
		{
			$this->constructor();
		}
		
		public function aquery($sql)
		{
		    $query_id = $this->query($sql);
			$this->num_rows = @mysql_num_rows($this->query_id);
		    $out = array();
		
		    while ($row = $this->fetch_array($query_id, $sql))
		        $out[] = $row;
		    
			$this->free_result($query_id);
		    return $out;
		}
		
		public function fetch_array($query_id=-1)
		{
		    ($query_id != -1) ? ($this->query_id = $query_id) : (TRUE);
		        
		    (isset($this->query_id)) ? ($this->record = @mysql_fetch_assoc($this->query_id)) : ($this->oops("Invalid query_id: <b>$this->query_id</b>. Records could not be fetched."));
		        
		    ($this->record) ? ($this->record = array_map("stripslashes", $this->record)) : (TRUE);
		
		   return $this->record;
		}
		
		public function oops($msg='') {

		    if($this->link_id>0)
		    {
		        $this->error=mysql_error($this->link_id);
		        $this->errno=mysql_errno($this->link_id);
		    }
		    else
		    {
		        $this->error=mysql_error();
		        $this->errno=mysql_errno();
		    }
		    echo '<table align="center" border="1" cellspacing="0" style="background:white;color:black;width:80%;">
		        <tr><th colspan=2>Database Error</th></tr>
		        <tr><td align="right" valign="top">Message:</td><td>'.$msg.'</td></tr>';
				
		    echo (strlen($this->error) > 0) ? ('<tr><td align="right" valign="top" nowrap>MySQL Error:</td><td>'.$this->error.'</td></tr></table>') : ('</table>');
		}
		
		
		public function query($sql)
		{	
			if ($sql == '')
			{
				echo 'We have an empty Query';
				return FALSE;
			}
			$start_time = microtime(true);
		    $this->query_id = @mysql_query($sql, $this->link_id);
		     
		    $this->affected_rows = @mysql_affected_rows();
			$this->last_id = mysql_insert_id();
			$this->query_counter++;
			$end_time = microtime(true);
			$ms = $start_time - $end_time;
		
		    return $this->query_id;
		}

		private function constructor()
		{
			$this->host=DB_HOST;
		    $this->user=DB_USER;
		    $this->pass=DB_PASS;
		    $this->database=DB_NAME;
			
			$this->link_id=@mysql_connect($this->host,$this->user,$this->pass);

		    if (!$this->link_id) {//open failed
		        $this->oops("Could not connect to server: <b>$this->host</b>.");
		    }
		
		    if(!@mysql_select_db($this->database, $this->link_id)) {//no database
		        $this->oops("Could not open database: <b>$this->database</b>.");
			}
		}
		
	}
?>